package com.example.milktea.controller;

import com.example.milktea.entity.Store;
import com.example.milktea.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/store")

public class StoreController {
    @Autowired
    private StoreService storeService;

    /*
        @RequestMapping("/insert")
        @ResponseBody
       /* public Store insert(HttpServletRequest request) {
            //新增
            Store store = new Store();

            String store_id=request.getParameter("store_id");
            Integer id = Integer.valueOf(store_id);
            String material_id=request.getParameter("material_id");
            Integer m_id = Integer.valueOf(material_id);

            String store_number=request.getParameter("store_number");
            Integer number = Integer.valueOf(store_number);
            String lack_flag=request.getParameter("lack_flag");
            Boolean flag=Boolean.valueOf(lack_flag);

            store.setStoreId(id);
            store.setStoreNumber(m_id);
            store.setStoreNumber(number);
            store.setLack_flag(flag);

            int result = this.storeService.insert(store);
            System.out.print("insert result:"+result);
            return store;
        }
    */
    @RequestMapping("/update")
    @ResponseBody
    public Store update(HttpServletRequest request) {
        Store store = new Store();

        String store_id=request.getParameter("store_id");
        Integer id = Integer.valueOf(store_id);
        String store_number=request.getParameter("store_number");
        //System.out.println("store_number:"+store_number);
        Integer number = Integer.valueOf(store_number);

        store.setStoreId(id);
        store.setStoreNumber(number);

        int result = this.storeService.update(store);
        System.out.print(result);
        return store;
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public List<Map<String, Object>> getAll(){
        List<Map<String, Object>> list =  this.storeService.getAll();
        return list;
    }


    @RequestMapping("/addMaterial")
    @ResponseBody
    public void addMaterial(HttpServletRequest request){
        String store_id = request.getParameter("store_id");
        this.storeService.addMaterial(Integer.valueOf(store_id));
    }

    @RequestMapping("/deleteMaterial")
    @ResponseBody
    public void deleteMaterial(HttpServletRequest request){
        String store_id = request.getParameter("store_id");
        this.storeService.deleteMaterial(Integer.valueOf(store_id));
    }


    @RequestMapping("/getAllLack")
    @ResponseBody
    public List<Map<String, Object>> getAllLack(){
        //获取缺货材料列表
        List<Map<String, Object>> list =  this.storeService.getAllLack();
        return list;
    }

}
